package com.scorpio.baselibrary.utils;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

import com.scorpio.baselibrary.utils.glide.GlideUtil;

/**
 * View处理控制器
 * Created by St on 2018/4/12.
 */
public class ViewController {
    private View mView;
    private SparseArray<View> views;
    private Context mContext;
    private Window mWindow;
    private int position = -1;

    public ViewController(Context context, Window window) {
        this.mContext = context;
        this.mWindow = window;
        views = new SparseArray<>();
    }

    public ViewController(Context context, View view) {
        this.mContext = context;
        this.mView = view;
        views = new SparseArray<>();
    }

    public Context getContext() {
        return mContext;
    }

    public View getLayoutView() {
        return mView;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public ViewController setGlideImageLoding(@IdRes int viewId, String url) {
        ImageView imageView = getView(viewId);
        GlideUtil.load(mContext, url, imageView);
        return this;
    }

    public ViewController addOnClickListener(View.OnClickListener listener, @IdRes int... viewIds) {
        for (int viewId : viewIds) {
            View view = getView(viewId);
            view.setOnClickListener(listener);
        }
        return this;
    }

    public String getText(@IdRes int viewId) {
        TextView view = getView(viewId);
        return view.getText().toString();
    }

    public <T extends View> T getView(@IdRes int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            if (mWindow != null) {
                view = mWindow.findViewById(viewId);
            } else if (mView != null) {
                view = mView.findViewById(viewId);
            } else {
                try {
                    throw new Exception("未初始化 父容器.");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            views.put(viewId, view);
        }
        return (T) view;
    }

    public ViewController setClickable(@IdRes int viewId, boolean clickable) {
        View view = getView(viewId);
        view.setClickable(clickable);
        return this;
    }


    public ViewController setText(@IdRes int viewId, String text) {
        TextView textView = getView(viewId);
        textView.setText(text);
        if (textView instanceof EditText) {
            ((EditText) textView).setSelection(textView.length());
        }
        return this;
    }

    public ViewController setEnabled(@IdRes int viewId, boolean enabled) {
        View textView = getView(viewId);
        textView.setEnabled(enabled);
        return this;
    }

    public ViewController setTextDrawableLeft(@IdRes int viewId, @DrawableRes int icon) {
        TextView textView = getView(viewId);
        textView.setCompoundDrawablesRelativeWithIntrinsicBounds(icon, 0, 0, 0);
        return this;
    }

    public ViewController setText(@IdRes int viewId, @StringRes int res_id) {
        TextView textView = getView(viewId);
        textView.setText(res_id);
        return this;
    }

    public ViewController setTextColor(@IdRes int viewId, @ColorInt int colorId) {
        TextView textView = getView(viewId);
        textView.setTextColor(colorId);
        return this;
    }
    public ViewController setTextSize(@IdRes int viewId, int size) {
        TextView textView = getView(viewId);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);
        return this;
    }
    public ViewController setImageTint(@IdRes int viewId, @ColorInt int colorId) {
        ImageView imageView = getView(viewId);
        imageView.setImageTintList(ColorStateList.valueOf(colorId));
        return this;
    }

    public ViewController setBackgroundColor(@IdRes int viewId, @ColorInt int colorId) {
        View view = getView(viewId);
        view.setBackgroundColor(colorId);
        return this;
    }

    public ViewController setRating(@IdRes int viewId, float rating, int max) {
        RatingBar view = getView(viewId);
        view.setMax(max);
        view.setRating(rating);
        return this;
    }

    public ViewController setRating(@IdRes int viewId, float rating) {
        RatingBar view = getView(viewId);
        view.setRating(rating);
        return this;
    }

    public ViewController setImageDrawable(@IdRes int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    public ViewController setImageDrawable(@IdRes int viewId, @DrawableRes int imgid) {
        ImageView view = getView(viewId);
        view.setImageResource(imgid);
        return this;
    }

    public ViewController setImageBitmap(@IdRes int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }


    public ViewController setBackgroundResource(@IdRes int viewId, @DrawableRes int backgroundResource) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundResource);
        return this;
    }

    public ViewController setGone(@IdRes int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public ViewController setInvisible(@IdRes int viewId, boolean invisible) {
        View view = getView(viewId);
        view.setVisibility(invisible ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public ViewController setImageResource(@IdRes int viewId, @DrawableRes int imageResId) {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
        return this;
    }

    public ViewController setProgress(@IdRes int viewId, int progress) {
        ProgressBar view = getView(viewId);
        view.setProgress(progress);
        return this;
    }

    public ViewController setTag(@IdRes int viewId, Object tag) {
        View view = getView(viewId);
        view.setTag(tag);
        return this;
    }

    public ViewController setTag(@IdRes int viewId, int key, Object tag) {
        View view = getView(viewId);
        view.setTag(key, tag);
        return this;
    }

    public ViewController setChecked(@IdRes int viewId, boolean checked) {
        View view = getView(viewId);
        if (view instanceof CompoundButton) {
            ((CompoundButton) view).setChecked(checked);
        } else if (view instanceof CheckedTextView) {
            ((CheckedTextView) view).setChecked(checked);
        }
        return this;
    }

    public boolean isChecked(@IdRes int viewId) {
        CompoundButton view = getView(viewId);
        return view.isChecked();
    }

    public ViewController setRecyclerViewAdapter(@IdRes int viewId, RecyclerView.Adapter adapter) {
        RecyclerView view = getView(viewId);
        view.setAdapter(adapter);
        return this;
    }

    public ViewController setListViewAdapter(@IdRes int viewId, BaseAdapter adapter) {
        ListView view = getView(viewId);
        view.setAdapter(adapter);
        return this;
    }

    public RecyclerView setRecyclerViewLayoutManager(@IdRes int viewId, RecyclerView.LayoutManager layoutManager) {
        RecyclerView view = getView(viewId);
        view.setLayoutManager(layoutManager);
        return view;
    }

    public ViewController scrollTo(@IdRes int viewId, int x, int y) {
        View view = getView(viewId);
        view.scrollTo(x, y);
        return this;
    }


    public TextView getTextView(@IdRes int viewId) {
        return getView(viewId);
    }

    public EditText getEditText(@IdRes int viewId) {
        return getView(viewId);
    }

    public ImageView getImageView(@IdRes int viewId) {
        return getView(viewId);
    }


}
